package com.leather.job.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.leather.common.entity.Job;
import com.leather.common.result.Result;
import com.leather.job.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class JobController {

    @Autowired
    private JobService jobService;


    @GetMapping("list")
    Object list(
            @RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
            @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
            @RequestParam(value = "search", required = false, defaultValue = "") String search
    ) {
        Map<String, Object> map = Maps.newHashMap();
        Page<Job> page = new Page<>(offset / 10 + 1, limit);
        IPage<Job> all = jobService.getAll(page, search);
        map.put("total", all.getTotal());
        map.put("rows", all.getRecords());
        return map;
    }


    @GetMapping("index")
    public Object index() {
        return jobService.jobIndex();
    }

    @PostMapping("addjob")
    public Object addJob(@RequestBody Job job) {
        return jobService.addJob(job);
    }

    @PostMapping("deletejob")
    public Object deleteJob(@RequestParam("jobId")int jobId) {
        return jobService.deleteJob(jobId);
    }

    @PostMapping("getjob")
    public Result getJob(int jobId){
        return jobService.getJobById(jobId);
    }

    @GetMapping("search")
    public Result search(String search) {
        return jobService.searchJob(search);
    }
}
